﻿using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Windows.Forms;
using IgnData.TileData;
using IgnData;


namespace WinTest
{
    public partial class FormIgnData : Form
    {
        //Graficos
        private const int nt = 10;
        private const int tileSize = 256;
        private Bitmap mapa = new Bitmap(nt * tileSize, nt * tileSize);
        private Bitmap grid = new Bitmap(nt * tileSize, nt * tileSize);
        private IgnMap ignMap;

        Graphics gr;
        Font drawFont = new Font("Arial", 12);
        SolidBrush drawBrush = new SolidBrush(Color.Black);


        //Referencias geograficas
        GeoCoord tileRefPoint = new GeoCoord();
        GeoCoord pxRefPoint = new GeoCoord();
        GeoCoord utmRefPoint = new GeoCoord();


        public FormIgnData()
        {
            InitializeComponent();
        }

        private void FormIgnData_Load(object sender, EventArgs e)
        {
            gr = Graphics.FromImage(mapa);
            pictureBox1.Height = mapa.Height;
            pictureBox1.Width = mapa.Width;
            pictureBox1.Image = mapa;
            gr.FillRectangle(new SolidBrush(Color.Aqua), 0, 0, mapa.Height, mapa.Width);
        }

        #region Respuesta a eventos

        private void btnLoad_IJ_Click(object sender, EventArgs e)
        {
            int iTile = int.Parse(toolStripTextBox1.Text);
            int jTile = int.Parse(toolStripTextBox2.Text);

            tileRefPoint.X = iTile;
            tileRefPoint.Y = jTile;

            pxRefPoint.X = iTile * 256;
            pxRefPoint.Y = jTile * 256;

            //Obtiene un rectangulo de 4 * 4
            for (int di = 0; di < 4; di++)
            {
                for (int dj = 0; dj < 4; dj++)
                {
                    Tile tile = TileManager.GetTile(tileRefPoint.X + di, tileRefPoint.Y - dj);
                    gr.DrawImage(tile.Bmp, di * 256, dj * 256);
                }
            }
            pictureBox1.Invalidate();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            mapa.Save(saveFileDialog1.FileName, ImageFormat.Jpeg);
        }

        private void btnLoad_XY_Click(object sender, EventArgs e)
        {
            int x = int.Parse(toolStripTextBox3.Text);
            int y = int.Parse(toolStripTextBox4.Text);
            Point utmPoint = new Point(x, y);
            GeoCoord centralPoint = new GeoCoord(x, y);
            ignMap = MapFactory.GetMapByTileSize(centralPoint, nt);
            
            //pictureBox1.Image = ignMap.Bmp;
            pictureBox1.DataBindings.Clear();
            pictureBox1.DataBindings.Add("Image", ignMap, "Bmp");

            ignMap.Mark(centralPoint);
            pictureBox1.Invalidate();

            this.utmRefPoint.X = (int)ignMap.Georeference.X;
            this.utmRefPoint.Y = (int)ignMap.Georeference.Y;
            pxRefPoint = IgnConverter.UtmToPx(utmRefPoint);
        }

        private void btnTileGrid_Click(object sender, EventArgs e)
        {
            this.DrawTileGrid();
            pictureBox1.Invalidate();
        }

        private void btnKmGrid_Click(object sender, EventArgs e)
        {
            ignMap.DrawKmGrid();
            pictureBox1.Invalidate();
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (ignMap != null)
            {
                GeoCoord mousePxPoint = new GeoCoord(pxRefPoint.X + e.X, pxRefPoint.Y - e.Y);
                GeoCoord mouseUtmPoint = IgnConverter.PxToUtm(mousePxPoint);
                slXX.Text = mouseUtmPoint.X.ToString("#,###") +"m";
                xlYY.Text = mouseUtmPoint.Y.ToString("#,###") + "m";
            }
        }

        private void eraseGridBtn_Click(object sender, EventArgs e)
        {
            ignMap.EraseKmGrid();
            pictureBox1.Invalidate();
        }
        #endregion



        private void DrawTileGrid()
        {
            using (Graphics tg = Graphics.FromImage(grid))
            {
                //tg.Clear(Color.White);
                Pen pen = new Pen(Color.Black, 1);
                for (int gi = 0; gi < 1024; gi += 256)
                {
                    tg.DrawLine(pen, gi, 0, gi, 1024);
                }
                for (int gj = 0; gj < 1024; gj += 256)
                {
                    tg.DrawLine(pen, 0, gj, 1024, gj);
                }
                for (int di = 0; di < 4; di++)
                {
                    for (int dj = 0; dj < 4; dj++)
                    {
                        string pos = string.Format("i:{0} j{1}", tileRefPoint.X + di, tileRefPoint.Y - dj);
                        tg.DrawString(pos, drawFont, drawBrush, di * 256, dj * 256);
                    }
                }
                gr.DrawImage(grid, 0, 0);
            }
        }





    }
}
